package com.mux.springboot.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/h2")
public class H2Controller {
	
	@Autowired
	JdbcTemplate JdbcTemplate;
	
	@RequestMapping("/findAll")
	public List<Map<String,Object>> findAll(){
		return	JdbcTemplate.queryForList("select * from dept");
		
	}
	
	@RequestMapping("/save")
	public String save(){
		String sql = "insert into dept (DEPTNAME) values (?)";
		JdbcTemplate.update(sql,"dept1");
		JdbcTemplate.update(sql,"dept2");
		JdbcTemplate.update(sql,"dept3");
		JdbcTemplate.update(sql,"dept4");
		return "ok";
	}
	
	@RequestMapping("/saveUser")
	public String saveUser(){
		String sql = "insert into user (deptid,name) values (?,?)";
		JdbcTemplate.update(sql,1,"devin1-1");
		JdbcTemplate.update(sql,1,"devin1-2");
		JdbcTemplate.update(sql,2,"devin2-1");
		JdbcTemplate.update(sql,2,"devin2-2");
		return "ok";
	}
	
	
	@RequestMapping("/findUserAll")
	public List<Map<String,Object>> findUserAll(int id){
		String sql = "select a.id,a.name,a.deptid,b.deptname from user a join dept b on a.deptid = b.id where a.id = ?";
		return JdbcTemplate.queryForList(sql, id);
	}
	
	
}
